package controller

import (
	"encoding/json"
	"goWeb/bookStore/dao"
	"goWeb/bookStore/model"
	"goWeb/bookStore/utils"
	"html/template"
	"net/http"
	"strconv"
)

func AddBookToCart(w http.ResponseWriter, r *http.Request) {
	flag, session := dao.IsLogin(r)
	if flag {
		//判断当前有没有购物车
		bookid := r.PostFormValue("bookid")
		ibookid, _ := strconv.ParseInt(bookid, 10, 64)
		book := dao.GetBookById(int(ibookid))
		userid := session.Userid
		cart, _ := dao.GetCartByUserId(userid)
		if cart != nil {
			//当前已经有购物车了
			//fmt.Println("当前已经有购物车")
			//fmt.Println(cart)
			//当前购物车里面有没有要购买的这一项
			cartItem, _ := dao.GetCartItemByBookIdAndCartId(bookid, cart.Id)
			if cartItem != nil {
				//购物车里有这个购物项
				//只需要找到这个购物项，把count+1即可
				//fmt.Println("购物车里有购物项")
				for _, v := range cart.CartItems {
					if v.Books.Id == cartItem.Books.Id {
						v.Count++
						dao.UpdateCartItemCount(v)
					}
				}
			} else {
				//fmt.Println("购物车里没有这个购物项")
				//购物车里没有这个购物项
				cartItem = &model.CartItem{
					Books:  book,
					Count:  1,
					CartId: cart.Id,
				}
				cart.CartItems = append(cart.CartItems, cartItem)
				dao.AddCartItem(cartItem)
			}
			dao.UpdateCart(cart)
		} else {
			//fmt.Println("当前还没有购物车")
			//当前还没有购物车
			//1、创建图书

			//2、创建cartItem
			//创建cartId
			cartId := utils.CreateUUID()
			cartItem := &model.CartItem{
				Books:  book,
				Count:  1,
				CartId: cartId,
			}
			//创建Cart
			var cartItems []*model.CartItem
			cartItems = append(cartItems, cartItem)
			cart = &model.Cart{
				Id:        cartId,
				Userid:    userid,
				CartItems: cartItems,
			}
			dao.AddCart(cart)
		}
		w.Write([]byte(book.Title))
	} else {
		w.Write([]byte("没有登录"))
	}

}

func ToCart(w http.ResponseWriter, r *http.Request) {
	flag, session := dao.IsLogin(r)
	if flag {
		//登录了
		//fmt.Println("已经登录了")
		cart, _ := dao.GetCartByUserId(session.Userid)
		if cart != nil {
			//有购物车
			//fmt.Println("已经有购物车了")
			session.Cart = cart
			t := template.Must(template.ParseFiles("views/pages/cart/cart.html"))
			t.Execute(w, session)
		} else {
			//没有购物车
			session.Cart = &model.Cart{}
			t := template.Must(template.ParseFiles("views/pages/cart/cart.html"))
			t.Execute(w, session)
		}

	} else {
		//没登录
		//fmt.Println("还没有登录")
		//w.Write(w, "没有登录")
		session := &model.Session{}
		t := template.Must(template.ParseFiles("views/pages/cart/cart.html"))
		t.Execute(w, session)
	}
}

//清空购物车
func EmptyCart(w http.ResponseWriter, r *http.Request) {
	cartId := r.FormValue("cartid")
	dao.EmptyCart(cartId)
	ToCart(w, r)
}

//根据id来删除购物项
func DeleteCartItem(w http.ResponseWriter, r *http.Request) {
	cartid := r.FormValue("ci")
	iCartid, _ := strconv.ParseInt(cartid, 10, 64)
	_, session := dao.IsLogin(r)
	cart, _ := dao.GetCartByUserId(session.Userid)
	for k, v := range cart.CartItems {
		if v.Id == int(iCartid) {
			cart.CartItems = append(cart.CartItems[:k], cart.CartItems[k+1:]...)
			dao.DelCartItemById(cartid)
		}
	}
	dao.UpdateCart(cart)
	ToCart(w, r)
}

//更新购物项中的数量
func ToUpdateCartCount(w http.ResponseWriter, r *http.Request) {
	cid := r.PostFormValue("cid")
	iCid, _ := strconv.ParseInt(cid, 10, 64)
	count := r.PostFormValue("count")
	iCount, _ := strconv.ParseInt(count, 10, 64)
	_, session := dao.IsLogin(r)
	cart, _ := dao.GetCartByUserId(session.Userid)

	for _, v := range cart.CartItems {
		if v.Id == int(iCid) {
			v.Count = int(iCount)
			dao.UpdateCartItemCount(v)
		}
	}
	dao.UpdateCart(cart)

	//ToCart(w, r)
	//再次获取购物车信息
	cart, _ = dao.GetCartByUserId(session.Userid)
	//将Tcount、Tamout和Count存入到Data结构体
	data := model.Data{
		Tcount: cart.Tcount,
		Tamout: cart.Tamout,
	}
	for _, v := range cart.CartItems {
		if v.Id == int(iCid) {
			data.Amout = v.Amout
		}
	}
	//将data序列化返回
	res, _ := json.Marshal(data)
	w.Write(res)
}
